<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>基础知识</title>
	<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css"" rel="stylesheet">
	<link href="https://cdn.bootcdn.net/ajax/libs/animate.css/3.2.3/animate.css" rel="stylesheet">
	<link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="../../static/css/public.css" />
</head>
<body>
	<!-- 回到顶部 -->
	<div class="topBtn">
		<a href="#top">
			<i class="fa fa-long-arrow-up"></i>
		</a>
	</div>
	<!--右边内容-->
	<div id="app" class="main animated fadeInRight">
		<div class="shadow">
			<div class="Table" v-cloak id="top">
				<div class="TableRow" v-for="(el,index) in list">
					<a class="RowLeft" :href="'#'+el.id">{{el.name}}</a>
					<span class="RowRight">{{el.explain}}</span>
				</div>
			</div>
			<div>
				<h4 id="typeof">call实现</h4>
				<pre>
	Function.prototype.callFun = function(context){
		context.fn = this
		context.fn(...Array.from(arguments).slice(1))
		/** 或者 context.fn(...params)   params 为context后面参数*/
		delete context.fn
	}
	
	let person = {
		name:'Miss-tree',
		detail:'an programmer'
	}
	
	function sayHi(age,sex){
		console.log(`${this.name} is ${this.detail}`,age,sex)
	}
	
	sayHi.callFun(person,'18','男')
	
	当call传入的对象是null的时候，或者其他一些类型的时候，函数会报错。
	<h5>call最后版本</h5>
	
	Function.prototype.callFun = function(context,...params){
		if (typeof context === 'object') {
			context = context || window 
		}else{
			context = Object.create(null)
		}
		let fn = Symbol()
		context[fn] = this
		context[fn](...params)
		delete context[fn]	
	}
	
	let person = {
		name:'Miss-tree',
		detail:'an programmer'
	}
	
	function sayHi(age,sex){
		console.log(this.name,this.detail,age,sex)
	}
	
	sayHi.callFun(person,'25','男')
	
	
	
				</pre>
			</div>
			<div>
				<h4 id="array">apply实现</h4>
				<pre>
	Function.prototype.newApply = function(context, parameter) {
	  if (typeof context === 'object') {
	    context = context || window
	  } else {
	    context = Object.create(null)
	  }
	  let fn = Symbol()
	  context[fn] = this
	  context[fn](...parameter);
	  delete context[fn]
	}
	
	let person = {
		name:'Miss-tree'
	}
	
	function sayHi(age,sex){
		console.log(this.name,age,sex)
	}
	
	sayHi.newApply(person,['35','男'])
	
				</pre>
			</div>
			<div>
				<h4 id="instanceof">bind实现</h4>
				<pre>
	Function.prototype.bind = function (context,...innerArgs) {
	  var me = this
	  return function (...finnalyArgs) {
	    return me.call(context,...innerArgs,...finnalyArgs)
	  }
	}
	/**
	 * Function.prototype.myBind = function (context, ...args) {
            let self = this
            function fn(...args) {
                context.fun = self    
                context.fun(...args)
            }

            return function () {
                return fn(...args)
            }
        }
	 */
	let person = {
		name:'Miss-tree'
	}
	function sayHi(age,sex) {
	  console.log(this.name, age, sex);
	}
	let personSayHi = sayHi.bind(person, 25)
	personSayHi('男')
			</pre>
		</div>
		</div>
	</div>
	<!--页面加载进度条-->
	<script src="https://cdn.bootcdn.net/ajax/libs/pace/0.5.0/pace.min.js"></script>
	<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.17/vue.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		var app = new Vue({
			el: "#app",
			data: function() {
				return {
					list: [
						{ name: "call实现",id: "typeof",explain: ''},
						{ name: "apply实现",id: "array",explain: ''},
						{ name: "bind实现",id: "instanceof",explain: ''},
					]
				}
			},
			mounted() {
			}
		})
	</script>
</body>

</html>